package ChooseLbView;

use strict;

use CGI qw(:standard);
use Athena::WebAPI::HTML;
use Athena::WebAPI::Form;
use Athena::WebAPI::Table;
use Athena::WebAPI::View;

use Athena::NavHeaderView;

use base ("View");

sub new {

    # Get the class name
    my $stInvocant = shift(@_);
    my $stClass = ref($stInvocant) || $stInvocant;

    # Create the current instance and bless into class
    my $rSelf = { };
    bless($rSelf,$stClass);

    my %hParams = ( @_ );
    $rSelf->{params} = \%hParams;

    return $rSelf;
}

sub draw {
  my $rSelf = shift(@_);

  my $rTable = Table->new(%{$rSelf->{params}->{table_style}});

  $rSelf->buildTable($rTable);

  my $rForm = Form->new(-method=>'post', -name=>'myForm');
  $rForm->addChild($rTable);
  $rForm->addHiddenFields($rSelf->{params}->{'hidden_fields'});

  my $rHTML = HTML->new();
  $rHTML->setBodyAttr(%{$rSelf->{params}->{body_style}});
  $rHTML->addChild($rForm);
  
  return $rHTML->draw();
}

sub buildTable {
   my $rSelf = shift(@_);
   my $rTable = shift(@_);
   my $iCurRow = 0;
   $rTable->addChild($iCurRow++, 0, NavHeaderView->new($rSelf->{params}));
   $rTable->addChild($iCurRow++, 0, "Choose Current LB",-bgcolor=>'#ddcccc');
   my $engine;
   my $first = $iCurRow;
   foreach $engine (keys(%{$rSelf->{params}->{engines}})) {
     my $EngineTable = Table->new(-align=>'left',
				  -border=>'0');
     my %labels = ("$engine"=>"");
     $EngineTable->addChild(0, 0, radio_group(-name=>'chosen_lb',
					      -values=>$engine,
					      -labels=>\%labels,
					      -default=>'-'),
			    -width=>'20'
			   );
     $EngineTable->addChild(0, 1, b($engine));
     $EngineTable->addChild(1, 0, "");
     $EngineTable->addChild(1, 1, "base url: " . 
			    $rSelf->{params}->{engines}->{$engine}->{url});
     $EngineTable->addChild(2, 0, "");
     $EngineTable->addChild(2, 1, "secondaries: " . 
			    $rSelf->{params}->{engines}->{$engine}->{secondaries});
     $EngineTable->addChild(3, 0, "");
     $EngineTable->addChild(3, 1, "refusing connections: " . 
			    $rSelf->{params}->{engines}->{$engine}->{refusing});
     $EngineTable->addChild(4, 0, "");
     $EngineTable->addChild(4, 1, "connection thread running: " . 
			    $rSelf->{params}->{engines}->{$engine}->{connected});
     $rTable->addChild($iCurRow++, 0, $EngineTable,
		       -bgcolor=>'#eeeeee');
   }

   $rTable->addChild($iCurRow++, 0, submit(-name=>'send'), 
		     -bgcolor=>'#ddcccc');
   $rTable->addChild($iCurRow++, 0, NavFooterView->new($rSelf->{params}));
 }

1;
